/*
* SimpContact.java
*
* Created on 5 Декабрь 2010 г., 13:39
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package protocol.simp;

import DrawControls.icons.Icon;

import chatty.Chatty;
import chatty.Options;
import chatty.chat.message.*;
import chatty.chat.message.PlainMessage;
import chatty.comm.StringConvertor;
import chatty.search.UserInfo;
import chatty.ui.TextListEx;
import chatty.ui.menu.MenuModel;

import protocol.*;

/**
*
* @author R.U.S.H.A.N.
*/
public class SimpContact extends Contact {

	private Client client = Client.noneClient;
	// #sijapp cond.if modules_XSTATUSES is "true" #
	private XStatus xstatus = XStatus.noneXStatus;
	// #sijapp cond.end #

	public SimpContact(Simp simp, String userid, String name) {
		protocol = simp;
		this.userId = userid;
		setGroupId(Group.NOT_IN_GROUP);
		this.setName(name);
		setOfflineStatus();
	}

	public void setOfflineStatus() {
		status = Status.offlineStatus;
		// #sijapp cond.if modules_XSTATUSES is "true" #
		xstatus = XStatus.noneXStatus;
		// #sijapp cond.end #
	}

	public String getStatusLineMessage() {
		// #sijapp cond.if modules_XSTATUSES is "true" #
		if (xstatus.getStatusIndex() > 0) {
			String message = xstatus.getMessage();
			if (!StringConvertor.isEmpty(message)) {
				return message;
			}
			message = xstatus.getName();
			if (!StringConvertor.isEmpty(message)) {
				return message;
			}
		}
		// #sijapp cond.end #
		return null;
	}
	/////////////////////////////////////////////////////////////////////////
	// #sijapp cond.if modules_XSTATUSES is "true" #
	public void getLeftIcons(Icon[] leftIcons) {
		if(getType() == CONTACT_TYPE_PHONE){
			leftIcons[0] = Simp.getPhoneContactIcon();
			return;
		}
		if (xstatus.getStatusIndex() > 0)
			leftIcons[1] = xstatus.getIcon();
			
		super.getLeftIcons(leftIcons);
	}
	// #sijapp cond.end #
	public void getRightIcons(Icon[] rightIcons) {
		rightIcons[0] = client.getIcon();
	}
	
	public Client getClient() {
		return client;
	}
	public void setClient(byte[] caps) {
		client = Client.createClient((Simp)getProtocol(), client, caps);
	}
	
	/////////////////////////////////////////////////////////////////////////
	public boolean isMeVisible() {
		return true;
	}

	public void showUserInfo() {
		UserInfo data = null;
		if (getProtocol().isConnected()) {
			data = ((Simp)getProtocol()).getConnection().getUserInfo(this);
			data.setProfileView(new TextListEx(getName()));
			data.setProfileViewToWait();

		} else {
			data = new UserInfo(getProtocol(), getUserId());
			data.uin = getUserId();
			data.nick = getName();
			data.setProfileView(new TextListEx(getName()));
			data.updateProfileView();
		}
		data.getProfileView().show();
	}

	public void showStatus(StatusView statusView) {
		//MenuModel menu = new MenuModel();
		
		statusView.init(this, getProtocol());
		statusView.addStatus(status);
		statusView.addStatusText(status.getText());
		// #sijapp cond.if modules_XSTATUSES is "true" #
		if (xstatus.getStatusIndex() > 0) {
			statusView.addStatus(xstatus.getIcon(), xstatus.getName());
			statusView.addStatusText(xstatus.getMessage());
		}
		// #sijapp cond.end #
		if (client.getIndex() != client.CLI_NONE) {
			statusView.addBr();
			statusView.addClient(client.getIcon(), client.getName());
		}
		statusView.showIt();
	}

	protected boolean sendSomeMessage(PlainMessage msg) {
		Simp o = (Simp)protocol;
		if (o.isConnected()) {
			o.getConnection().sendMessage(msg);
		}
		return true;
	}
	
	// #sijapp cond.if modules_XSTATUSES is "true" #
	public final void setXStatus( byte xIndex, String xstatusDesc) {
		if (-1 == xIndex) {
			xstatus = XStatus.noneXStatus;
		} else {
			xstatus = new XStatus((byte)xIndex, xstatusDesc);
		}
	}
	
	public XStatus getXStatus() {
		return xstatus;
	}
	// #sijapp cond.end #
	
	void setStatus(byte statusIndex, String statusText) {
		if (Status.I_STATUS_OFFLINE == statusIndex) {
			status = Status.offlineStatus;
			return;
		}
		status = new Status();
		status.setStatusIndex(statusIndex);
		status.setText(statusText);
	}
	
	// #sijapp cond.if modules_XSTATUSES is "true" #
	public void requestXStatusMessage() {
		//setXtrazMessage("", "");
	}
	// #sijapp cond.end #
	
	public void requestStatusMessage() {
		if (isOnline()) {
		}
	}
	public MenuModel getContextMenu() {
		contactMenu.clean();
		addChatItems(contactMenu);
		if(getType() != CONTACT_TYPE_PHONE){
			contactMenu.addItem("info", Chatty.icons.iconAt(Chatty.ICON_USER_INFO), USER_MENU_USER_INFO);
		}
		addFileTransferItems(contactMenu);
		contactMenu.addItem("manage", Chatty.icons.iconAt(Chatty.ICON_MANAGE_CONTACT), USER_MANAGE_CONTACT);
		// #sijapp cond.if modules_HISTORY is "true" #
		contactMenu.addItem("history", Chatty.icons.iconAt(Chatty.ICON_HISTORY), Contact.USER_MENU_HISTORY);
		// #sijapp cond.end#
		contactMenu.addItem("copy_login", Chatty.chatIcons.iconAt(Chatty.ICON_COPY), Contact.USER_MENU_COPY_LOGIN);
		
		if(getType() != CONTACT_TYPE_PHONE){
			contactMenu.addItem("extra", Chatty.icons.iconAt(Chatty.ICON_EXTRA), Contact.USER_MENU_EXTRA);
		}
		if (isOnline()) {
			contactMenu.addItem("user_statuses", USER_MENU_STATUSES);
		}

		contactMenu.setActionListener(this);
		return contactMenu;
	}
	protected void initManageContactMenu(MenuModel menu) {
		if (protocol.isConnected()) {
			if (isTemp()) {
				menu.addEllipsisItem("add_user", Chatty.icons.iconAt(Chatty.ICON_USER_ADD), USER_MENU_ADD_USER);
			}
			if ((protocol.getGroupItems().size() > 1) && !isTemp()) {
				menu.addEllipsisItem("move_to_group", Chatty.icons.iconAt(Chatty.ICON_USER_MOVE), USER_MENU_MOVE);
			}
			if (!isAuth()) {
				menu.addEllipsisItem("requauth", Message.msgIcons.iconAt(Message.ICON_SYS_OK), USER_MENU_REQU_AUTH);
			}
			menu.addItem("grand_future_auth", Chatty.icons.iconAt(Chatty.ICON_AUTH_GRANT), USER_MENU_GRANT_FUTURE_AUTH);
			menu.addEllipsisItem("remove_me", Chatty.icons.iconAt(Chatty.ICON_USER_REMOVE_ME), USER_MENU_REMOVE_ME);
		}
		if ((protocol.isConnected() || isTemp()) && protocol.inContactList(this)) {
			menu.addEllipsisItem("rename", Chatty.icons.iconAt(Chatty.ICON_USER_RENAME), USER_MENU_RENAME);
			menu.addSeparator();
			menu.addEllipsisItem("remove", Chatty.icons.iconAt(Chatty.ICON_USER_REMOVE), USER_MENU_USER_REMOVE);
		}
	}
	
	public void doAction(int action) {
		super.doAction(action);
		switch (action) {
		case USER_MENU_REQU_AUTH: /* Request auth */
			((Simp)getProtocol()).requestAuth(getUserId());
			resetAuthRequests();
			Chatty.setPrevDisplay();
			break;

		case USER_MENU_GRANT_FUTURE_AUTH:
		case USER_MENU_GRANT_AUTH:
			((Simp)getProtocol()).grandAuth(getUserId());
			resetAuthRequests();
			Chatty.setPrevDisplay();
			break;

		case USER_MENU_DENY_AUTH:
			((Simp)getProtocol()).denyAuth(getUserId());
			resetAuthRequests();
			Chatty.setPrevDisplay();
			break;
			
		case USER_MENU_REMOVE_ME:
			((Simp)getProtocol()).removeMe(getUserId());
			break;	
		}
	}
}